home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 February: Tool Chest / Apple Developer CD Series Tool Chest February 1996 (Apple Computer)(1996).iso / Tool Chest / Testing & Debugging / Virtual User tools / SPEC S&L v.1.0.1 / Libraries / FileTool.Lib < prev    next >
Encoding:
Text File  |  1993-12-17  |  18.6 KB  |  589 lines  |  [TEXT/MPS ]

  1. #
  2. # ****************************************************************************
  3. #
  4. #    File Name:    FileTool.Lib
  5. #
  6. #    Contains:    Derived from FileTool.vulib
  7. #
  8. #    Written by:    NAGA, Kevin Avoy et al
  9. #
  10. #    Copyright:    © 1993 by Apple Computer, Inc., all rights reserved.
  11. #
  12. # ****************************************************************************
  13. #            C h a n g e        H i s t o r y (most recent first):
  14. # ****************************************************************************
  15. #
  16. #        Vers      Date        Author        Description
  17. #        ----    --------    ------    ---------------------------------------------
  18. #     <1.0.2>     12/6/93    RTV        Synched original & SPEC S&L vers of this lib
  19. #     <1.0.1>     5/21/93    KTA        Added ReadLine2(), ExistsOrCreate(), InitFileTool()
  20. #     <1.0.0>     5/21/93    NAGA    Created
  21. #
  22. # ****************************************************************************
  23. #
  24.  
  25. tool FileTool s:'FILE'
  26. begin
  27.     # Services specific to FileTool:
  28.     
  29.     # To create a file
  30.     Service    "Create"( 'string', 'string', 'string' ) return 'integer';
  31.     # first parameter is the Fullpath name for the file 
  32.     # second parameter is the 4 character signature (creator eg., 'MPS ') 
  33.     # third parameter is the 4 character file type (eg., 'TEXT') 
  34.     # return value is the file reference number for future calls
  35.     
  36.     # To create a folder
  37.     Service    "CreateFolder"( 'string' ) return 'integer';
  38.     # first parameter is the Fullpath name for the file 
  39.     # return value is the file reference number for future calls
  40.     
  41.     # To delete a file/folder
  42.     Service    "Delete"( 'string', 'integer' );
  43.     # first parameter is the Fullpath name for the file (can be '', if second
  44.     # parameter is given non zero value)
  45.     # second parameter if non-zero is the file reference previously 
  46.     # returned by the tool
  47.     # once a file/folder is deleted its reference number is no longer valid
  48.  
  49.     # To erase the contents of the data fork of a file
  50.     Service    "Erase"( 'string', 'integer' ) return 'integer';
  51.     # first parameter is the Fullpath name for the file (can be '', if second
  52.     # parameter is given non zero value)
  53.     # second parameter if non-zero is the file reference previously 
  54.     # returned by the tool
  55.  
  56.     # To check if a file exists
  57.     Service    "FileExists"( 'string' ) return 'integer';
  58.     # first parameter is the Fullpath name for the file (can be '', if second
  59.     # parameter is given non zero value)
  60.     # the return value is 1 if file exists else 0
  61.  
  62.     # To check if a file exists
  63.     Service    "CopyFile"( 'string', 'integer', 'string' ) return 'integer';
  64.     # first parameter is the Fullpath name for the file (can be '', if second
  65.     # parameter is given non zero value)
  66.     # second parameter if non-zero is the file reference previously 
  67.     # returned by the tool
  68.     # third parameter is the new file name (not a path name)
  69.     # the return value is 1 if file copied else 0
  70.  
  71.     # To get back memory in the FileTool
  72.     Service    "ForgetFile"( 'string', 'integer' ) return 'integer';
  73.     # first parameter is the Fullpath name for the file (can be '', if second
  74.     # parameter is given non zero value)
  75.     # second parameter if non-zero is the file reference previously 
  76.     # returned by the tool
  77.     # the return value is insignificant
  78.  
  79.     # To move a file/folder
  80.     Service    "Move"( 'string', 'integer', 'string' ) return 'integer';
  81.     # first parameter is the Fullpath name for the file (can be '', if second
  82.     # parameter is given non zero value)
  83.     # second parameter if non-zero is the file reference previously 
  84.     # returned by the tool
  85.     # third parameter is the new file path name
  86.     # return value is the file reference number for future calls
  87.  
  88.     # To read from a file a specified number of characters
  89.     Service    "Read"( 'string', 'integer', 'integer', 'integer' ) return 'string';
  90.     # first parameter is the Fullpath name for the file (can be '', if second
  91.     # parameter is given non zero value)
  92.     # second parameter if non-zero is the file reference previously 
  93.     # returned by the tool
  94.     # third parameter is the number of characters to be read
  95.     # fourth parameter is the number of characters from start of file after which to start reading
  96.     # return value is the string read from the file
  97.  
  98.     # To read from a file until a specified character is seen
  99.     Service    "ReadUntil"( 'string', 'integer', 'string', 'integer' ) return 'string';
  100.     # first parameter is the Fullpath name for the file (can be '', if second
  101.     # parameter is given non zero value)
  102.     # second parameter if non-zero is the file reference previously 
  103.     # returned by the tool
  104.     # third parameter is the single character strinng that indicates the
  105.     # character until which the contents of a file has to be read
  106.     # fourth parameter is the number of characters from start of file after which to start reading
  107.     # return value is the string read from the file
  108.  
  109.     # To rename a file/folder
  110.     Service    "Rename"( 'string', 'integer', 'string' ) return 'integer';
  111.     # first parameter is the Fullpath name for the file (can be '', if second
  112.     # parameter is given non zero value)
  113.     # second parameter if non-zero is the file reference previously 
  114.     # returned by the tool
  115.     # third parameter is the new file name (not a path name)
  116.     # return value is the file reference number for future calls
  117.  
  118.     # To get back the list of mounted volume names
  119.     Service    "VolumeList"() return 'list';
  120.     
  121.     # To write to the data fork of a file
  122.     Service    "Write"( 'string', 'integer', 'string' ) return 'integer';
  123.     # first parameter is the Fullpath name for the file (can be '', if second
  124.     # parameter is given non zero value)
  125.     # second parameter if non-zero is the file reference previously 
  126.     # returned by the tool
  127.     # third parameter is the text to be appended to (written at the end
  128.     # of) the file specified by the first two parameters
  129.     # return value is the file reference number for future calls
  130.  
  131.     # To append the second file to the first, both must be text files
  132.     Service    "AppendFile"( 'string', 'string' ) return 'symbol';
  133.  
  134.     # To locate the special folders defined by the system
  135.     # first parameter is the four character code for the folder, the FolderType
  136.     # the full path name to the folder is returned 
  137.     Service    "FindFolder"( 'string' ) return 'string';
  138.  
  139.     # Read the short & long version string from the specified file
  140.     Service    "ReadVersion"( 'string', 'integer' ) return 'list';
  141.  
  142.     # Test if a resource exists within a specified file, return true if so
  143.     Service    "ResourceExists"( 'string', 'string', 'integer' ) return 'symbol';
  144.  
  145.     # Services common to all Virtual User external tools:
  146.     Service "Initialize"( 'undefined' );        # pass TRUE for target, FALSE for host
  147.     Service "Cancel"( 'string' );
  148.     Service "GetToolServices"() return 'list';
  149.     Service "GetToolVersion"() return 'list';
  150.     Service "ServiceSupported"( 'string' ) return 'undefined';
  151.     Service "Quit"();
  152.     
  153. end;
  154.  
  155. task CreateFile( pFilePathName, pSignature := 'MPS ', pType := 'TEXT' )
  156. begin
  157.     return FileTool( "Create", pFilePathName, pSignature, pType ); 
  158. end;
  159.  
  160. task CreateFolder( pFolderPathName )
  161. begin
  162.     return FileTool( "CreateFolder", pFolderPathName ); 
  163. end;
  164.  
  165. task DeleteFile( pFileReference )
  166. begin
  167.     if( typeOf( pFileReference ) = 'integer' )
  168.     begin
  169.         tFilePathStr := '';
  170.         tFileReferenceNumber := pFileReference;
  171.     end;
  172.     else if( typeOf( pFileReference ) = 'string' )
  173.     begin
  174.         tFilePathStr := pFileReference;
  175.         tFileReferenceNumber := 0;
  176.     end;
  177.     else
  178.         return -50;#gParamError = -50, same as what the tool returns
  179.     
  180.     # Now call the tool with tFilePathStr, tFileReferenceNumber as parameters
  181.     return FileTool( "Delete", tFilePathStr, tFileReferenceNumber ); 
  182.     
  183. end;
  184.  
  185. # This task will empty the contents of the specified file (dataFork only)
  186. # the file will not be deleted
  187. task EraseFile( pFileReference )
  188. begin
  189.     if( typeOf( pFileReference ) = 'integer' )
  190.     begin
  191.         tFilePathStr := '';
  192.         tFileReferenceNumber := pFileReference;
  193.     end;
  194.     else if( typeOf( pFileReference ) = 'string' )
  195.     begin
  196.         tFilePathStr := pFileReference;
  197.         tFileReferenceNumber := 0;
  198.     end;
  199.     else
  200.         return -50;#gParamError = -50, same as what the tool returns
  201.     
  202.     # Now call the tool with tFilePathStr, tFileReferenceNumber as parameters
  203.     return FileTool( "Erase", tFilePathStr, tFileReferenceNumber ); 
  204.     
  205. end;
  206.  
  207. task FileExists( pFilePathName )
  208. begin
  209.     if( typeOf( pFilePathName ) <> 'string' )
  210.     begin
  211.         return -50;#gParamError = -50, same as what the tool returns
  212.     end;
  213.     
  214.     # Now call the tool with pFilePathName as parameter
  215.     return FileTool( "FileExists", pFilePathName ); 
  216.     
  217. end;
  218.  
  219. task FileCopy( pFileReference, pDestPathName )
  220. begin
  221.     if( typeOf( pFileReference ) = 'integer' )
  222.     begin
  223.         tFilePathStr := '';
  224.         tFileReferenceNumber := pFileReference;
  225.     end;
  226.     else if( typeOf( pFileReference ) = 'string' )
  227.     begin
  228.         tFilePathStr := pFileReference;
  229.         tFileReferenceNumber := 0;
  230.     end;
  231.     else
  232.         return -50;#gParamError = -50, same as what the tool returns
  233.     
  234.     # Now call the tool with tFilePathStr, tFileReferenceNumber and pDestPathName as parameters
  235.     return FileTool( "CopyFile", tFilePathStr, tFileReferenceNumber, pDestPathName ); 
  236.     
  237. end;
  238.  
  239. # This task will free up memory in the FileTool
  240. # the file will not be touched or affected in any way
  241. task ForgetFile( pFileReference )
  242. begin
  243.     if( typeOf( pFileReference ) = 'integer' )
  244.     begin
  245.         tFilePathStr := '';
  246.         tFileReferenceNumber := pFileReference;
  247.     end;
  248.     else if( typeOf( pFileReference ) = 'string' )
  249.     begin
  250.         tFilePathStr := pFileReference;
  251.         tFileReferenceNumber := 0;
  252.     end;
  253.     else
  254.         return -50;#gParamError = -50, same as what the tool returns
  255.     
  256.     # Now call the tool with tFilePathStr, tFileReferenceNumber as parameters
  257.     return FileTool( "ForgetFile", tFilePathStr, tFileReferenceNumber ); 
  258.     
  259. end;
  260.  
  261. task MoveFile( pFileReference, pNewPathName )
  262. begin
  263.     if( typeOf( pFileReference ) = 'integer' )
  264.     begin
  265.         tFilePathStr := '';
  266.         tFileReferenceNumber := pFileReference;
  267.     end;
  268.     else if( typeOf( pFileReference ) = 'string' )
  269.     begin
  270.         tFilePathStr := pFileReference;
  271.         tFileReferenceNumber := 0;
  272.     end;
  273.     else
  274.         return -50;#gParamError = -50, same as what the tool returns
  275.     
  276.     # Now call the tool with tFilePathStr, tFileReferenceNumber and pNewPathName as parameters
  277.     return FileTool( "Move", tFilePathStr, tFileReferenceNumber, pNewPathName ); 
  278.     
  279. end;
  280.  
  281. task RenameFile( pFileReference, pNewName )
  282. begin
  283.     if( typeOf( pFileReference ) = 'integer' )
  284.     begin
  285.         tFilePathStr := '';
  286.         tFileReferenceNumber := pFileReference;
  287.     end;
  288.     else if( typeOf( pFileReference ) = 'string' )
  289.     begin
  290.         tFilePathStr := pFileReference;
  291.         tFileReferenceNumber := 0;
  292.     end;
  293.     else
  294.         return -50;#gParamError = -50, same as what the tool returns
  295.     
  296.     # Now call the tool with tFilePathStr, tFileReferenceNumber and pNewName as parameters
  297.     return FileTool( "Rename", tFilePathStr, tFileReferenceNumber, pNewName ); 
  298.     
  299. end;
  300.  
  301. task VolumeList()
  302. begin
  303.     
  304.     # Now call the tool 
  305.     return FileTool( "VolumeList" ); 
  306.     
  307. end;
  308.  
  309. # a utility task used by WriteToFile
  310. task ToText( pData )
  311. begin
  312.     if( typeOf( pData ) = 'string' ) return pData;
  313.     if( typeOf( pData ) = 'integer' ) return NumToStr( pData );
  314.     return undefined; # else
  315. end;
  316.  
  317. # This task will append pData to end of the specified file
  318. # pData can a string or a list of strings and numbers
  319. task WriteToFile( pFileReference, pData )
  320. begin
  321.     tText := '';
  322.     if( typeOf( pFileReference ) = 'integer' )
  323.     begin
  324.         tFilePathStr := '';
  325.         tFileReferenceNumber := pFileReference;
  326.     end;
  327.     else if( typeOf( pFileReference ) = 'string' )
  328.     begin
  329.         tFilePathStr := pFileReference;
  330.         tFileReferenceNumber := 0;
  331.     end;
  332.     else
  333.         return -50;#gParamError = -50, same as what the tool returns
  334.     
  335.     if( typeOf( pData ) = 'list' )
  336.     begin
  337.         for i := 1 to card pData
  338.         begin
  339.             tNext := ToText( pData[i] );
  340.             if( tNext )
  341.                 tText := tText + tNext;
  342.             else
  343.                 return -50;
  344.         end;
  345.     end;
  346.     else if( typeOf( pData ) = 'string' )
  347.     begin
  348.         tText := pData;
  349.     end;
  350.     else
  351.         return -50;#gParamError = -50, same as what the tool returns
  352.  
  353.     # Now call the tool with tFilePathStr, tFileReferenceNumber and tText as parameters
  354.     return FileTool( "Write", tFilePathStr, tFileReferenceNumber, tText ); 
  355.  
  356. end;
  357.  
  358.  
  359. task Read( pFileReference, pNumOfCharacters, pFromPosition := 0 )
  360. begin
  361.     if( typeOf( pFileReference ) = 'integer' )
  362.     begin
  363.         tFilePathStr := '';
  364.         tFileReferenceNumber := pFileReference;
  365.     end;
  366.     else if( typeOf( pFileReference ) = 'string' )
  367.     begin
  368.         tFilePathStr := pFileReference;
  369.         tFileReferenceNumber := 0;
  370.     end;
  371.     else
  372.         return -50;#gParamError = -50, same as what the tool returns
  373.     
  374.     # Now call the tool with tFilePathStr, tFileReferenceNumber and pNumOfCharacters as parameters
  375.     return FileTool( "Read", tFilePathStr, tFileReferenceNumber, pNumOfCharacters, pFromPosition ); 
  376.     
  377. end;
  378.  
  379. task ReadUntil( pFileReference, pUntilCharacter, pFromPosition := 0 )
  380. begin
  381.     if( typeOf( pFileReference ) = 'integer' )
  382.     begin
  383.         tFilePathStr := '';
  384.         tFileReferenceNumber := pFileReference;
  385.     end;
  386.     else if( typeOf( pFileReference ) = 'string' )
  387.     begin
  388.         tFilePathStr := pFileReference;
  389.         tFileReferenceNumber := 0;
  390.     end;
  391.     else
  392.         return -50;#gParamError = -50, same as what the tool returns
  393.     
  394.     # Now call the tool with tFilePathStr, tFileReferenceNumber and pUntilCharacter as parameters
  395.     return FileTool( "ReadUntil", tFilePathStr, tFileReferenceNumber, pUntilCharacter, pFromPosition ); 
  396.     
  397. end;
  398.  
  399. task ReadLine( pFileReference, pFromPosition := 0 )
  400. begin
  401.     return ReadUntil( pFileReference, "∂n", pFromPosition );
  402. end;
  403.  
  404.  
  405.  
  406.  
  407. task AppendFile( pTargetFile, pSourceFile )
  408. begin
  409.     if not ( typeOf( pTargetFile ) = 'string' )
  410.     begin
  411.         return { -50 };#gParamError = -50, same as what the tool returns
  412.     end;
  413.     
  414.     if not ( typeOf( pSourceFile ) = 'string' )
  415.     begin
  416.         return { -50 };#gParamError = -50, same as what the tool returns
  417.     end;
  418.         
  419.     # Now call the tool with tTargetFile, tSourceFile
  420.     x := FileTool( "AppendFile", pTargetFile, pSourceFile ); 
  421.     return x;
  422. end;
  423.  
  424. task FindFolder( FolderType )
  425. begin
  426.     if not ( typeOf( FolderType ) = 'string' )
  427.     begin
  428.         return { errAEWrongParameters, undef, "Bad FolderType parameter" };
  429.     end;
  430.     
  431.     if ( card( FolderType ) > 4 ) or ( card( FolderType ) <= 0 ) 
  432.     begin
  433.         return { errAEWrongParameters, undef, "Bad FolderType parameter" };
  434.     end;
  435.     
  436.     x := FileTool( "FindFolder", FolderType ); 
  437.     return x;
  438. end;
  439.  
  440. #########################################################################
  441. #        InitFileTool(pOnTheTarget := true)
  442. #========================================================================
  443. # Author:        Kevin Avoy (x4-5604)
  444. # Description:    Initializes the FileTool 
  445. # Parameters:    pOnTheTarget - True if you want to launch the FileTool on  
  446. #                                the target machine. False if on the Host.
  447. # Returns:        Nothing
  448. # Examples:        InitFileTool();
  449. # Assumptions:    None 
  450. #========================================================================
  451. # History:
  452. #
  453. #########################################################################
  454. TASK InitFileTool(pOnTheTarget := true)
  455. begin
  456.     returnVal := 0;
  457.     FileToolInit :=  FileTool("Initialize", pOnTheTarget );         # Launch FileTool on Host
  458.     if FileToolInit[1] <> 0                                    # If error during initialization,
  459.     begin
  460.         println "FileTool could not be initialized";        # print error
  461.         println "Error ", FileToolInit[1], ". ",  FileToolInit[3] ;
  462.     end;
  463.     else
  464.     begin
  465.         println "FileTool Initialized properly";
  466.         returnVal := 1;
  467.     end;
  468.     
  469.     return(returnVal);
  470. end;
  471.  
  472.  
  473. #########################################################################
  474. #        ReadLine2( pFileReference, pFromPosition )
  475. #========================================================================
  476. # Author:        Kevin Avoy (x4-5604)
  477. # Description:    Similar to ReadLine Above except this routine does the error
  478. #                checking and will return 0 if an error occur otherwise it will 
  479. #                what the line that was read (2nd element of what theFileTool returned)
  480. # Parameters:    pFileReference - File to read from.  
  481. #                pFromPosition - position to begin reading from.
  482. # Returns:        0 - Some Error
  483. #                String - line read
  484. # Examples:        ReadLine2('hd:myFile', 0);
  485. # Assumptions:    None 
  486. #========================================================================
  487. # History:
  488. #
  489. #########################################################################
  490. task ReadLine2( pFileReference, pFromPosition := 0 )
  491. begin
  492.     theLine := ReadUntil( pFileReference, "∂n", pFromPosition );
  493.     if(theLine[1] <> 0)
  494.     begin 
  495.         println "Problem reading the file";        # print error
  496.         println "Error ", theLine[1], ". ",  theLine[3] ;
  497.         returnVal := 0;
  498.     end;
  499.     else
  500.         returnVal := theLine[2];
  501.     return (returnVal);
  502. end;
  503.  
  504.  
  505. #########################################################################
  506. #                        ExistsOrCreate()
  507. #========================================================================
  508. # Author:        Kevin Avoy
  509. # Description:    Checks to see if the <pFileName> exists - creates one if it does not.
  510. # Parameters:    pFileName - full path of file
  511. # Returns:        0 - Fail (doesn't exist, couldn't create)
  512. #                1 - File Existed 
  513. #                2 - Created File 
  514. # Examples:        ExistsOrCreate();
  515. # Assumptions:    None 
  516. #========================================================================
  517. # History:
  518. #
  519. #########################################################################
  520. task ExistsOrCreate(pFileName)
  521. begin
  522.     returnVal := 0;
  523.     ###    Create the file only if it doesn't exist
  524.     x := FileExists( pFileName );
  525.     if not (TypeOf( x ) = 'list' and x[1] = 0)
  526.     begin
  527.         ###    Create the File
  528.         CreatedFile := CreateFile( pFileName );
  529.         If(CreatedFile[1] <> 0)
  530.         begin
  531.             Println "!@#$% An error occured while creating the file";
  532.             Println "Error", CreatedFile[1], CreatedFile[3];
  533.         end;
  534.         else    # File was created
  535.             returnVal := 2;
  536.     end;
  537.     else        # File must Exist
  538.         returnVal := 1;
  539.  
  540.     return(returnVal);
  541. end;
  542.  
  543.  
  544. #########################################################################
  545. #                        ReadVersion()
  546. #========================================================================
  547. # Author:        Rick Violet
  548. # Description:    Returns the short & long version strings from the 'vers' resource
  549. # Parameters:    FilePathName     - full path of file
  550. #                pVersID         - 'vers' resource's id
  551. # Returns:        undefined         - if resource not found
  552. #                { shortString, longString } - if successful
  553. # Examples:        ReadVersion();
  554. # Assumptions:    None 
  555. #========================================================================
  556. # History:
  557. #
  558. #########################################################################
  559. task ReadVersion( FilePathName, pVersID := 1 )
  560. begin
  561.     x := FileTool( "ReadVersion", FilePathName, pVersID ); 
  562.     SErr := ScriptError();
  563.     return { x[1], x[2], x[3], SErr };
  564. end;
  565.  
  566. #########################################################################
  567. #                        ResourceExists()
  568. #========================================================================
  569. # Author:        Rick Violet
  570. # Description:    Checks to see if the resource exists.
  571. # Parameters:    FilePathName - full path of file
  572. #                pResType      - resource's type
  573. #                pResType      - resource's ID
  574. # Returns:        0 - Fail (doesn't exist )
  575. #                1 - resource Exists
  576. # Examples:        ResourceExists();
  577. # Assumptions:    None 
  578. #========================================================================
  579. # History:
  580. #
  581. #########################################################################
  582. task ResourceExists( FilePathName, pResType, pResID )
  583. begin
  584.     x := FileTool( "ResourceExists", FilePathName, pResType, pResID ); 
  585.     SErr := ScriptError();
  586.     return { x[1], x[2], x[3], SErr };
  587. end;
  588.  
  589.